import { Notification } from 'element-ui'

var showNotify = function (title,message,type){
    Notification({
        title: title,
        message: message,
        type : type,
        position: 'bottom-right'
    });
}

var showSuccess = function (title,message){
    this.showNotify(title,message,'success');
}

var showError = function (title,message){
    this.showNotify(title,message,'error');
}

var showWarning = function(title,message){
    this.showNotify(title,message,'warning');
}

var showInfo = function(title,message){
    this.showNotify(title,message,);
}

var showAxiosResult = function(result,sucFunc,errFunc){
    if(result.code === 200){
        this.showSuccess(result.message);
        if(typeof sucFunc == 'function') sucFunc();
    }else{
        this.showError('',result.message)
        if(typeof errFunc == 'function') errFunc();
    }
}

function isEmpty(obj){
    if(typeof obj == "undefined" || obj == null || obj == ""){
        return true;
    }
    if(Array.prototype.isPrototypeOf(obj) && obj.length === 0){
    	return true;
    }
    if(Object.prototype.isPrototypeOf(obj) && Object.keys(obj).length === 0) {
    	return true;
    }
    return false;
}

function getColList(selectData,colName){
    if(Array.prototype.isPrototypeOf(selectData) && selectData.length !== 0){
        let returnList = []
        for(let i = 0 ; i < selectData.length ; i++){
            returnList[i] = selectData[i][colName]
        }
    	return returnList;
    }else{
        return [];
    }
}

function getFidList(selectData){
    if(Array.prototype.isPrototypeOf(selectData) && selectData.length !== 0){
        let returnList = []
        for(let i = 0 ; i < selectData.length ; i++){
            returnList[i] = selectData[i].fid
        }
    	return returnList;
    }else{
        return [];
    }
}

//获取分录list
function getEntryList(selectData){
    if(Array.prototype.isPrototypeOf(selectData) && selectData.length !== 0){
        let returnList = []
        for(let i = 0 ; i < selectData.length ; i++){
            returnList[i] = selectData[i].entry
        }
    	return returnList;
    }else{
        return [];
    }
}

//获取分录ID
function getEntryFidList(selectData){
    if(Array.prototype.isPrototypeOf(selectData) && selectData.length !== 0){
        let returnList = []
        for(let i = 0 ; i < selectData.length ; i++){
            returnList[i] = selectData[i].entry.fid
        }
    	return returnList;
    }else{
        return [];
    }
}

//获取分录源单ID
function getOEntryFidList(selectData){
    if(Array.prototype.isPrototypeOf(selectData) && selectData.length !== 0){
        let returnList = []
        for(let i = 0 ; i < selectData.length ; i++){
            returnList[i] = selectData[i].entry.foid
        }
    	return returnList;
    }else{
        return [];
    }
}

function removeEmptyKey(selectData){
    let newArr = [];//去除空对象
    for(let j in selectData){
        for(var key in selectData[j]){
            if(selectData[j][key] ==''){
            delete selectData[j][key]
            }
        }
        newArr.push(selectData[j]);
    };
    return newArr;
}

function getArrayIndex(arrays,obj,key){
    var i = arrays.length;
    while (i--) {
        if (arrays[i][key] === obj[key]) {
            return i;
        }
    }
    return false;
}

function deepClone(obj) {
    let result = typeof  obj.splice === "function" ? [] : {};
    if (obj && typeof obj === 'object') {
        for (let key in obj) {
            if (obj[key] && typeof obj[key] === 'object') {
                result[key] = deepClone(obj[key]);//如果对象的属性值为object的时候，递归调用deepClone,即在吧某个值对象复制一份到新的对象的对应值中。
            } else {
                result[key] = obj[key];//如果对象的属性值不为object的时候，直接复制参数对象的每一个键值到新的对象对应的键值对中。
            }

        }
        return result;
    }
    return obj;
}

function formatDate ({ cellValue }) {
    if(isEmpty(cellValue))
        return cellValue
    else
        return cellValue.length > 10 ? cellValue.substring(0, 10) : cellValue;
}

function el_formatDate(row, column){
    let cellValue = getValueByPath(row,column.property)
    if(isEmpty(cellValue))
        return cellValue
    else
        return cellValue.length > 10 ? cellValue.substring(0, 10) : cellValue;
}

/**
 * 获取大括号内部变量的值
 * @param {*} data 对象
 * @param {string} path 路径
 */
 function getValueByPath(data, path) {
    const paths = path.split('.');
    let res = data;
    for (const path of paths) {
      res = res[path];
    }
    return res;
  }

function el_formatBillStatus(row, column){
    let cellValue = getValueByPath(row,column.property)
    let style = ""
    if(cellValue == '暂存')
        style = '';
    else if(cellValue == '提交' || cellValue=='未关闭')
        style =  'success';
    else if(cellValue == '生效' || cellValue=='已关闭')
        style =  'warning';
    else if(cellValue =='关闭' || cellValue == '作废')
        style =  'info';
    return '<el-tag :type="'+style+'" disable-transitions>'+cellValue+'</el-tag>'
}

export default {showInfo,showAxiosResult,showNotify,showSuccess,showError,showWarning,getOEntryFidList,getColList,
    isEmpty,getFidList,removeEmptyKey,deepClone,getEntryFidList,getEntryList,getArrayIndex,formatDate,el_formatDate,getValueByPath,el_formatBillStatus};